from selenium.webdriver.common.by import By
from Page.base_page import BuyerBasePage
from Common.read_verifycode import verify_code
from config import Basedir
import logging


class LoginPage(BuyerBasePage):
    def __init__(self):
        super().__init__()
        # 账号输入框
        self.username_box = (By.ID, 'username')
        # 密码输入框
        self.password_box = (By.ID, 'password')
        # 验证码图片区域
        self.code_img_box = (By.ID, 'verify_code_img')
        # 验证码输入框
        self.code_box = (By.ID, 'verify_code')
        # 点击登录按钮
        self.login_bth = (By.XPATH, '//*[@class="login_bnt"]/a')

        self.name = (By.XPATH, '//div/a[@class="mu-m-phone"]')
        # 错误信息提示框
        self.err_msg = (By.CSS_SELECTOR, '.layui-layer-content.layui-layer-padding')

    def login(self, username, password):
        # 截取验证码
        code_img = Basedir + '/Img/login.png'
        self.el_screenshot(self.find_el(*self.code_img_box), code_img)

        # 读取验证码
        code = verify_code(code_img)

        # 输入信息
        self.input_text(self.find_el(*self.username_box), username)
        self.input_text(self.find_el(*self.password_box), password)
        self.input_text(self.find_el(*self.code_box), code)

        # 点击登录
        self.find_el(*self.login_bth).click()

    def is_err_exist(self):
        if self.is_el_exist(*self.err_msg):
            pass

